package thread_pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/5/19
 * Time:19:54
 * 自定义线程池行为
 **/
public class PoolDemo2 {
    public static void main(String[] args) {

        //线程工厂
        MyThreadFactory myThreadFactory = new MyThreadFactory();
        //创建线程池
        ExecutorService service = Executors.newFixedThreadPool(10,myThreadFactory);

        for (int i = 0; i < 10; i++) {
            service.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("线程名称："+ Thread.currentThread().getName()+
                            ",线程优先级"+Thread.currentThread().getPriority());
                }
            });
        }

    }
    public static int count = 1;
    static class MyThreadFactory implements ThreadFactory{

        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            //设置线程池的命名
            thread.setName("myThreadPool-"+count++);
            //设置线程池的优先级
            thread.setPriority(10);
            return thread;
        }
    }
}
